
// 定义一个数值和字符串的联合类型

let objNum:(Number|string)[]=[1,2,'1','2']
console.log(objNum);

// 定义一个类型断言

let dyarr:number[]=[1,2,3,4];
let dy:number=(dyarr as number[]).length;
console.log(dy);

// 定义一个 void 实现函数的无返回值
function fnc():void{
    console.log("好好学习天天向上");
}
fnc();

// 定义一个 never 的函数

function infiniteLoop():never{
    while(true){

    }
}


// 分别定义 es5 和 ts 的函数定义

function fnkx(a:number,b?:number){
    if(b){
        return console.log(a+b);
    }
    else{
        return console.log(a);
        
    }
}
fnkx(1)
function fn(a:number,b:number){
    return console.log(a+b);
}
fn(1,2)
function fn1(a1:string,b1='b1'){
    return console.log(a1+''+b1);
}
fn1("a1")
function buildName(firstName:string,...restOfName:string[]){
    return console.log(firstName+""+restOfName.join(""));
}
buildName("Joseph", "Samuel", "Lucas", "MacKinzie")

// 实现一个函数重载
let suits = ["hearts", "spades", "clubs", "diamonds"];

function pickCard(x: {suit: string; card: number; }[]): number;
function pickCard(x: number): {suit: string; card: number; };
function pickCard(x: any) {
    if (typeof x == "object") {
        let pickedCard = Math.floor(Math.random() * x.length);
        return pickedCard;
    }
    else if (typeof x == "number") {
        let pickedSuit = Math.floor(x / 13);
        return { suit: suits[pickedSuit], card: x % 13 };
    }
}
let myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }];
let pickedCard1 = myDeck[pickCard(myDeck)];
alert("card: " + pickedCard1.card + " of " + pickedCard1.suit);

let pickedCard2 = pickCard(15);
alert("card: " + pickedCard2.card + " of " + pickedCard2.suit);

// ts 中定义一个类并实现继承

class Animal {
    move(distanceInMeters: number = 0) {
        console.log(`Animal moved ${distanceInMeters}m.`);
    }
}

class Dog extends Animal {
    bark() {
        console.log('Woof! Woof!');
    }
}

const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();


// 2种方式定义一个数组（数组中的元素可以是任意类型）

let arr:any[]=[1,"one",true];
let arr1:[number,string,boolean]=[1,'as',true];
let arr2:{a:number,b:string,c:boolean}[]=[
    {a:1,b:"as",c:true},
    {a:2,b:"bs",c:false}
]

// Ts定义一个求和函数;例Add(2,8,9,6,5,8,7),返回结果45

function Add(val1:number,...val:number[]){
    let sum:number=0;
    sum+=val1;
    if(val)
        val.forEach((item,index)=>{
            sum+=item
        })
    return sum
}
console.log(Add(2,8,9,6,5,8,7));


// Ts定义一个函数返回值为boolean类型的判断函数;例isTrue(nudefined),返回false
function isTrue(val:any){
    switch(typeof val){
        case "number":
            return true
            break;
        case "undefined":
            return false
            break;
        case "object":
            return true
            break
        default:
            break;
    }
}
console.log(isTrue(undefined));


// 使用函数编写，实现统计指定字符串中指定字符出现的次数，字符串和要统计的字母作为参数传递。str="acdkjcdeicl"  ;  strCount(str, “c”) 
// 思路：计数器： 遍历每一个字符。判断是否和val全等，如果全等，计数器加一。

function sumItem(val:string,ele:string){
    let num:number=0;
    let arr=val.split("")
    arr.forEach((item:string,index:number)=>{
        if(item===ele){
            num+=1
        }
    })
    return num;
}
console.log(sumItem("asdededsfg","e"));
console.log(sumItem("qwasdederqwdsfg","e"));

// 数字（乘5）添加：
// 将arrs中所有的数字乘以5 var arrs = [‘a’,’c’,6,’e’,5,3,’d’]，返回[30,25,15]

function Addnum(val:any[]){
    let newArr:number[]=[]
    val.forEach((v:string,i:number)=>{
        if(typeof v==='number'){
            newArr.push(v*5)
        }
    })
    return newArr
}
var arrs:any[]=["a","c",6,"e",5,3,"d"]
var arrq:any[]=["a",12,6,"e",5,3,"d"]
console.log(Addnum(arrs));
console.log(Addnum(arrq));

// 补零：
// 写一个函数，将这个数组中所有的一位数前面补0，输出一个新数组，即最后返回。

function Adderxo(val:number[]){
    let newArr:any[]=[]
    val.forEach((item,index)=>{
        let items:string="0"+item
        newArr.push(items)
    })
    return newArr
}
let arro:number[]=[2,3,45,6,7]
console.log(Adderxo(arro));


// 找真值：
// 写一个函数bouncer（），实现将数组中的真值，添加到一个新数组中。
function bouncer(val:any[]){
    let newArr:any[]=[]
    val.forEach((v,i)=>{
        if(Boolean(v)&&typeof v!=='boolean'){
            newArr.push(v)
        }
    })
    return newArr
}
let arra:any[]=['1',2,'asw',45,true,"",0]
console.log(bouncer(arra));
console.log(bouncer([7,"ate","",false,9]));

